package com.example.noval.ui.read

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.noval.R
import com.example.noval.databinding.ActivityReadBinding
import com.example.noval.db.mode.ContentResp
import com.example.noval.db.mode.TextPage
import com.example.noval.db.mode.TocResp
import com.example.noval.network.repository.BookRepository
import com.example.noval.network.repository.SearchRepository
import com.example.noval.ui.adapter.ReadBookAdapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ReadActivity : AppCompatActivity() {
    
    private var binding:ActivityReadBinding?=null
    
    private var adpater:ReadBookAdapter?=null
    private var bookRepository=BookRepository()
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= DataBindingUtil.setContentView(this,R.layout.activity_read)
//        setContentView(R.layout.activity_read)
        val bundle = intent.extras
        var contents = bundle?.getSerializable("contents") as ArrayList<ContentResp>
   
        contents.forEach{
            it.
        }
        
        
   
        val tocs = bundle?.getSerializable("tocDataList") as ArrayList<TocResp>
        Log.d("zzl",contents.toString())
        Log.d("zzl",tocs.toString())
        val bookId=tocs!![0].bookId
        var chapterIndex:Int=0
        adpater= ReadBookAdapter(contents)
        
        
        
//        adpater?.setOnItemClick(object :ReadBookAdapter.OnItemClickListener{
//            override fun onItemClick(position: Int) {
//                GlobalScope.launch(Dispatchers.IO) {
//                    if(chapterIndex<tocs.size){
//                        chapterIndex+=1
//                    }
//                    var toc=tocs[chapterIndex]
//                    Log.d("zzl","readbookadapter")
//                    contents= bookRepository.searchContent(toc.bookId,toc.chapterId,toc.contentMd5) as ArrayList<ContentResp>
//                    withContext(Dispatchers.Main){
//                        adpater?.updateDataList(contents)
//                    }
//                }
//                
//                Log.d("zzl","readbookadapter1111")
//            }
//        })
        binding?.recyclerView?.adapter=adpater
        binding?.recyclerView?.layoutManager=LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        
    }
}